<?php
namespace Models\Model;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Predicate;
use Zend\Db\Sql\Expression;
class OrderDeliveryDetailsTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	public function getShopOrders($shop_id){        
		$select = $this->tableGateway->getSql()->select();
		$select->join('user_medicine_order', new Expression('user_medicine_order.user_medicine_order_id=order_delivery_details.user_medicine_order_id'),array('*'),'left');
		$select->join('user', new Expression('user.user_id=user_medicine_order.user_id'),array('user_id','user_name'),'left');
		$select->join('user_details', new Expression('user_details.user_id=user.user_id'),array('city','state','country','address'),'left');		
		$select->where('order_delivery_details.shop_id="'.$shop_id.'"');                                         
		$resultSet = $this->tableGateway->selectWith($select);		
		return $resultSet;
    }
	public function updateOrderPrice($order_delivery_details_id,$price){
	   $data = array(
		   'price'      =>$price,
		);
	   $result=$this->tableGateway->update($data, array('order_delivery_details_id' => $order_delivery_details_id));	   
	   return $result;                        
   }
   public function assignOrdersToShops($order)
   {
		$shopIds=explode('***',$order['shop_ids']);
		foreach($shopIds as $shopId)
		{
			$result[]=$this->saveOrder($shopId,$order['user_medicine_order_id'],$order['discount']);
		}
		return $result;
   }
   public function saveOrder($shopId,$orderId,$discount)
   {
		$data = array(		
			'shop_id'          					=> 	$shopId,  		
			'user_medicine_order_id'          	=> 	$orderId,		
			'discount'	          				=> 	$discount,
			'date_added'						=>  date('Y-m-d H:i:s')
		);	
		$insertresult=$this->tableGateway->insert($data);	
		return $this->tableGateway->lastInsertValue;
   }
   public function updateOderWithQutation($shopOrderId,$order)
   {
		$data = array(		
			'price'				=>	$order['price'],	  	
			'shop_order_status'	=>	1,
			'date_updated'		=>	date('Y-m-d H:i:s'), 	
		);	
		$updateuserid=$this->tableGateway->update($data, array('order_delivery_details_id' => $shopOrderId));
		return $updateuserid;
   }
    public function updateShopOrdersByAdmin($order)
   {
		$orderIds=explode('***',$order['order_delivery_details_id']);
		foreach($orderIds as $orderId)
		{
			$data = array(		
			'admin_process_status'	=>	$order['status'],
			'date_updated'		=>	date('Y-m-d H:i:s'), 	
		);	
		$updateuserid=$this->tableGateway->update($data, array('order_delivery_details_id' => $orderId));
		$result[]=$updateuserid;
		}
		return $result;
   }
   public function getShopReports($shop_id){	
		$select = $this->tableGateway->getSql()->select();
		$select->join(array('user'=>'order_delivery_details'), 'order_delivery_details.order_delivery_details_id=user.order_delivery_details_id',array('counts'=>new Expression('COUNT(user.shop_order_status)')),'left');
		$select->Group('user.shop_order_status');
		$select->where('user.shop_id="'.$shop_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);	
		return $resultSet;		
	}
}